package com.example.myapplication;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private String []mTitles = new String[]{"tab1","tab2","tab3","tab4","tab5","tab6","tab7","tab8","tab9"};
    private TabLayout mTab;
    private ViewPager mVp;
    private List<Fragment> mFragments;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        mFragments = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            Bundle bundle = new Bundle();
            bundle.putString("key",mTitles[i]);
            Fragment fragment = new MyFragment();
            fragment.setArguments(bundle);
            mFragments.add(fragment);

        }
        MyAdapter adapter = new MyAdapter(getSupportFragmentManager(),mTitles,mFragments);
        mVp.setAdapter(adapter);
        mTab.setupWithViewPager(mVp);

    }

    private void findViews() {
        mTab =  findViewById(R.id.tablayout);
        mVp = findViewById(R.id.pager);
    }
}
